package org.example.com.controller;

import jakarta.annotation.Resource;
import org.example.com.Bean.User;
import org.example.com.service.UserService;
import org.example.com.util.JsonResult;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class userController {
    @Resource
    private UserService userService;
    @GetMapping
    public JsonResult findAll() {
        return userService.findUser();
    }
    @GetMapping("/page/{page}")
    public JsonResult find(@PathVariable("page") int page){
        return userService.findUser(page);
    }
    @GetMapping("/id/{id}")
    public JsonResult find(@PathVariable("id") String id){
         return userService.findById(id);
    }
   @GetMapping("/search/{username}/{page}")
    public JsonResult find(@PathVariable("username") String username,@PathVariable("page") int page){
        return userService.findByUserName(username,page);
    }
    @PostMapping
    public JsonResult save(User user){
        return userService.saveUser(user);
    }
    @PutMapping
    public JsonResult update(User user){
        return userService.updateUser(user);
    }
    @PutMapping("/change_pass/{id}/{pass}")
    public JsonResult updateUserPassword(@PathVariable("id") String id,@PathVariable("pass") String newPass){
        return userService.updateUserPassword(id, newPass);
    }
    @DeleteMapping("/{id}")
    public JsonResult deleteUser(@PathVariable("id") String id){
        return userService.deleteUser(id);
    }
    @PutMapping("/change_status/{id}/{status}")
    public JsonResult update(@PathVariable("id") String id,@PathVariable("status") int status){
        return userService.updateUserStatus(id,status);
    }
    @GetMapping("/password_exists/{id}/{password}")
    public JsonResult checkPasswordExists(@PathVariable("id") String id, @PathVariable("password") String password) {
        return userService.checkPasswordExists(id, password);
    }
    @PostMapping("/login")
    public JsonResult login(User user){
        return userService.login(user);
    }
}
